package demo.db.pg;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import demo.java.sql.JdbcDemo;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class PostgreSqlDemo {

    public static String jdbcUrl = "jdbc:postgresql://dify-postgresql-master.ximalaya.hw:5432/dify";
    public static String user = "postgres";
    public static String password = "difyai123456";
    public static String Driver = "org.postgresql.Driver";


    public static Connection connect() throws SQLException {
        return DriverManager.getConnection(jdbcUrl, user, password);
    }

    public static Connection getConnection() throws SQLException {
        HikariDataSource ds = buildDataSource();
        return ds.getConnection();
    }

    public static HikariDataSource buildDataSource() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(jdbcUrl);
        config.setUsername(user);
        config.setPassword(password);
        config.setMaximumPoolSize(10);
        return new HikariDataSource(config);
    }


    @Test
    public void testQuery() {
        String sql = "select * from apps where id = 'f70205a2-d729-4f88-b1d9-24bfe281e6dd'";
        try (Connection connect = connect()) {
            System.out.println("连接 PostgreSQL 成功");
            PreparedStatement preparedStatement = connect.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            JdbcDemo.printResultSet(resultSet);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
